// Copyright  2004-2008 Backplane Ltd.
//
// This file is part of Ubiquity formsPlayer
//
// Ubiquity formsPlayer is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3 of the License, or (at your
// option) any later version.
//
// Ubiquity formsPlayer is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License
// along with Ubiquity formsPlayer If not, see <http://www.gnu.org/licenses/>.
//	
//
//   Purpose: Implementation of CImplActionToggle.
//   $Id$

#include "stdafx.h"
#include "actiontoggle.h"
#include "idResolver.h"
#include "XFormsElementToggle.h"
#include "ImplContextualValue.h"
#include "case.h"
#include "XFormsElement.h"
#include "XFormsXFormsElement.h"

/*
CImplActionToggle::CImplActionToggle()
:m_pCaseResolver(0)
{

}*/

CImplActionToggle::CImplActionToggle(CXFormsElementToggle * pXFEl)
:m_pCaseResolver(0)
{
	m_pXFormsElement = pXFEl;
	pXFEl->AddRef();

}

CImplActionToggle::~CImplActionToggle()
{
	delete m_pCaseResolver;
}
HRESULT CImplActionToggle::PerformAction()
{
	HRESULT hr = S_FALSE;

	if(m_pXForms)
		m_pXForms->RunDeferredUpdate();
	CImpl * pImpl = 0;
	if(m_pCaseResolver)
	{
		CComBSTR sCase;
		CComPtr<IXMLDOMNode> pContext;
		GetXPathContext(&pContext);
		m_pCaseResolver->GetValue(m_pModel,pContext,&sCase);
		hr = GetIdResolver()->getItem(sCase,pImpl);
	}
	else
		hr = GetIdResolver()->getItem(m_sCase,pImpl);

	if(SUCCEEDED(hr) && !IsAwaitingDeletion())
	{
		CImplCase * pCase = dynamic_cast<CImplCase*>(pImpl);
		if(pCase)
		{
			hr = pCase->toggle();
		}
		else
		{
			hr = E_FAIL;
		}

	}
//	hr = m_pXForms->toggle(m_sCase);
	return hr;
}
